package com.project.platform.service.impl;

import com.alibaba.fastjson2.JSONObject;
import com.project.platform.dto.CurrentUserDTO;
import com.project.platform.dto.RetrievePasswordDTO;
import com.project.platform.dto.UpdatePasswordDTO;
import com.project.platform.entity.Elder;
import com.project.platform.entity.Family;
import com.project.platform.exception.CustomException;
import com.project.platform.mapper.FamilyMapper;
import com.project.platform.service.FamilyService;
import com.project.platform.utils.CurrentUserThreadLocal;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import com.project.platform.vo.PageVO;

import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * ${table.name}
 */
@Service
public class FamilyServiceImpl implements FamilyService {
    @Resource
    private FamilyMapper familyMapper;

    @Value("${resetPassword}")
    private String resetPassword;

    @Override
    public PageVO<Family> page(Map<String, Object> query, Integer pageNum, Integer pageSize) {
        PageVO<Family> page = new PageVO();
        List<Family> list = familyMapper.queryPage((pageNum - 1) * pageSize, pageSize, query);
        page.setList(list);
        page.setTotal(familyMapper.queryCount(query));
        return page;
    }

    @Override
    public CurrentUserDTO login(String username, String password) {
        Family family = familyMapper.selectByUserName(username);
        if (family == null || !family.getPassword().equals(password)) {
            throw new CustomException("用户名或密码错误");
        }
        if (family.getStatus().equals("禁用")) {
            throw new CustomException("用户已禁用");
        }
        CurrentUserDTO currentUserDTO = new CurrentUserDTO();
        BeanUtils.copyProperties(family, currentUserDTO);
        return currentUserDTO;
    }

    @Override
    public void register(JSONObject data) {
        Family family = new Family();
        family.setUsername(data.getString("username"));
        family.setNickname(data.getString("nickname"));
        family.setAvatarUrl(data.getString("avatarUrl"));
        family.setPassword(data.getString("password"));
        family.setStatus("启用");
        insert(family);

    }

    @Override
    public void updateCurrentUserInfo(CurrentUserDTO currentUserDTO) {
        Family family = familyMapper.selectById(currentUserDTO.getId());
        family.setNickname(currentUserDTO.getNickname());
        family.setAvatarUrl(currentUserDTO.getAvatarUrl());
        family.setTel(currentUserDTO.getTel());
        family.setEmail(currentUserDTO.getEmail());
        familyMapper.updateById(family);
    }

    @Override
    public void updateCurrentUserPassword(UpdatePasswordDTO updatePassword) {
        Family family = familyMapper.selectById(CurrentUserThreadLocal.getCurrentUser().getId());
        if (!family.getPassword().equals(updatePassword.getOldPassword())) {
            throw new CustomException("旧密码不正确");
        }
        family.setPassword(updatePassword.getNewPassword());
        familyMapper.updateById(family);
    }

    @Override
    public void resetPassword(Integer id) {
        Family family = familyMapper.selectById(id);
        family.setPassword(resetPassword);
        familyMapper.updateById(family);
    }

    @Override
    public void retrievePassword(RetrievePasswordDTO retrievePasswordDTO) {
        Family family = familyMapper.selectByTel(retrievePasswordDTO.getTel());
        if (family == null) {
            throw new CustomException("手机号不存在");
        }
        family.setPassword(retrievePasswordDTO.getPassword());
        familyMapper.updateById(family);
    }

    @Override
    public Family selectById(Integer id) {
        return familyMapper.selectById(id);
    }

    @Override
    public List<Family> list() {
        return familyMapper.list();
    }

    @Override
    public void insert(Family entity) {
        check(entity);
        familyMapper.insert(entity);
    }

    @Override
    public void updateById(Family entity) {
        check(entity);
        familyMapper.updateById(entity);
    }

    private void check(Family entity) {
        Family family = familyMapper.selectByUserName(entity.getUsername());
        if (family != null && !Objects.equals(family.getId(), entity.getId())) {
            throw new CustomException("用户名已存在");
        }
    }

    @Override
    public void removeByIds(List<Integer> ids) {
        familyMapper.removeByIds(ids);
    }
}
